#region Summary

// 
// Owner : JackieHan 
// 
// Solution : ResearchTest 
// 
// Project : TestSerializer
// 
// File : TestNormalSerializer.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 一般的测试
// 
// History : 
//			2011-04-15 11:06:19	    Create 
// 

#endregion

#region Using

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using College;
using HighSchool;
using NUnit.Framework;
using System.Xml.Serialization;
using SharpTestsEx;

#endregion

namespace TestSerializer
{
    /// <summary> 
    /// 一般的测试
    /// </summary>
    [TestFixture]
    public class TestXmlSerializer
    {
        [Test]
        public void TestNormal() //一般化的对象序列化，输出xml都有命名空间
        {
            var orderItem = new OrderItem { ItemName = "Widget", Description = "Regular Widget", Quantity = 10, UnitPrice = 2.3M };
            var serializer = new XmlSerializer(typeof(OrderItem));
            var stream = new MemoryStream();
            serializer.Serialize(stream, orderItem);
            stream.Position = 0;
            var reader = new StreamReader(stream);
            var xmlStr = reader.ReadToEnd();
            Console.WriteLine(xmlStr);
        }

        [Test]
        public void TestWithXmlTypeMapping() //不懂
        {
            var mapping = new SoapReflectionImporter().ImportTypeMapping(typeof(Group));
            var serializer = new XmlSerializer(mapping);
            var group = Group.GetDefalutInstance();

            var writer = new XmlTextWriter("soapOriginal.xml", Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartElement("wrapper");

            serializer.Serialize(writer, group);
            writer.WriteEndElement();
            writer.Close();

            Process.Start("soapOriginal.xml");
        }

        [Test]
        public void TestWithDefaultNamespace() //输出xml有命名空间
        {
            var orderItem = new OrderItem { ItemName = "Widget", Description = "Regular Widget", Quantity = 10, UnitPrice = 2.3M };
            var serializer = new XmlSerializer(typeof(OrderItem), "http://api.s.cn");
            var stream = new MemoryStream();
            serializer.Serialize(stream, orderItem);
            stream.Position = 0;
            var reader = new StreamReader(stream);
            var xmlStr = reader.ReadToEnd();
            Console.WriteLine(xmlStr);
        }

        /// <summary>
        /// Tearch的属性Info属性是一个Object数组，由于不知道Object的具体类型，一般序列化方式不能序列化
        /// 用扩展类型，就可以序列化
        /// </summary>
        [Test]
        public void TestWithExtraType()
        {
            var extraType = new Type[3] { typeof(Address), typeof(PhoneInfo), typeof(InteranationalPhone) };
            var serializer = new XmlSerializer(typeof(Tearcher), extraType);
            var stream = new MemoryStream();
            serializer.Serialize(stream, Tearcher.GetDefaultInstance());
            stream.Position = 0;
            var reader = new StreamReader(stream);
            var xmlStr = reader.ReadToEnd();
            Console.WriteLine(xmlStr);

            serializer = new XmlSerializer(typeof(Tearcher));
            stream = new MemoryStream();
            serializer.Serialize(stream, Tearcher.GetDefaultInstance());
            stream.Position = 0;
            reader = new StreamReader(stream);
            xmlStr = reader.ReadToEnd();
            Console.WriteLine(xmlStr);
        }

        /// <summary>
        /// 重写序列化
        /// </summary>
        [Test]
        public void TestWithOverride()
        {
            var attrOverrides = new XmlAttributeOverrides();
            var attrs = new XmlAttributes();
            //var attr = new XmlElementAttribute("Alumni", typeof (Graduate));
            //attrs.XmlElements.Add(attr);

            var arrayAttr = new XmlArrayAttribute("StudentList");
            var arrayItemAttr = new XmlArrayItemAttribute("Student", typeof(Graduate));
            attrs.XmlArray = arrayAttr;
            attrs.XmlArrayItems.Add(arrayItemAttr);

            attrOverrides.Add(typeof(HighSchool.MyClass), "Students", attrs);

            var mySerializer = new XmlSerializer
           (typeof(HighSchool.MyClass), attrOverrides);

            var myClass = new MyClass();

            var g1 = new Graduate { Name = "Jackie", Id = 1, University = "Alma Mater" };

            var g2 = new Graduate { Name = "Megan", Id = 2, University = "CM" };

            Student[] myArray = { g1, g2 };
            myClass.Students = myArray;

            var stream = new MemoryStream();
            mySerializer.Serialize(stream, myClass);
            stream.Position = 0;
            var reader = new StreamReader(stream);
            var xmlStr = reader.ReadToEnd();
            Console.WriteLine(xmlStr);


            var serializer = new XmlSerializer(typeof(MyClass));
            stream = new MemoryStream();
            serializer.Serialize(stream, myClass);
            stream.Position = 0;
            reader = new StreamReader(stream);
            xmlStr = reader.ReadToEnd();
            Console.WriteLine(xmlStr);

        }

        [Test]
        public void TestTestsSerialize()
        {
            var myClass = new TextAttributeClass { Text = new string[] { "aaaa", "bbb", "ccc" }, id = 1, name = "jackieHan" };
            var serializer = new XmlSerializer(typeof(TextAttributeClass));
            var stream = new MemoryStream();
            serializer.Serialize(stream, myClass);
            stream.Position = 0;
            var reader = new StreamReader(stream);
            var xmlStr = reader.ReadToEnd();
            Console.WriteLine(xmlStr);
        }
    }
}